package yxy.game.pm2.module.pickedtroops;

import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;

import java.util.HashMap;

//兵符槽
public class PickedTroopsModule extends BaseGameRedisLockBean {
    // bean===========================================================================
    private String playerCode;

    private HashMap<Integer, PickedTroops> pts = new HashMap<Integer, PickedTroops>(); //key阵营  value兵符槽


    public HashMap<Integer, PickedTroops> getPts() {
        return pts;
    }

    public void setPts(HashMap<Integer, PickedTroops> pts) {
        this.pts = pts;
    }

    public String getPlayerCode() {
        return playerCode;
    }

    public void setPlayerCode(String playerCode) {
        this.playerCode = playerCode;
    }

// func===========================================================================

    private static PickedTroopsModule create(String playerCode) {
        PickedTroopsModule module = new PickedTroopsModule();
        module.setPlayerCode(playerCode);

        //初始化6个阵容的兵符信息
        for (int i = 1; i <= 6; i++) {
            PickedTroops pickedTroops = new PickedTroops();
            pickedTroops.setCamp(i);
            pickedTroops.setOpen(false);
            pickedTroops.setLevel(1);
            pickedTroops.setStagelevel(0);
            pickedTroops.setDamageincrease(0);
            pickedTroops.setDamagedrop(0);
            pickedTroops.setPickedTroopsSolt("");
            module.getPts().put(i,pickedTroops);
        }
        module.set();
        return module;
    }

    // redis==========================================================================
    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:PICKEDTROOPS", playerCode);
    }

    public void set() {
        try {
            setv(key(getPlayerCode()), this);
            check();
        } catch (RedisException e) {
            e.printStackTrace();
        }

    }

    static public PickedTroopsModule get(String playerCode) {

        try {
            PickedTroopsModule module = getv(key(playerCode));

            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), PickedTroopsModule.class);
                if(module != null){module.set();}//把数据同步到redis
            }

            //todo 后期放数据库
            if (module == null) {
                module = create(playerCode);
            }
            module.check();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //红点
    private void check() {

    }


    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                PickedTroopsModule pickedTroopsModule = getv(key(playerCode));
                if(pickedTroopsModule != null)MySQLCacheDAO.setV(key, pickedTroopsModule);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }

    }
    // lock==========================================================================

    static private String lockKey(String playerCode) {
        return String.format("lock:%s", key(playerCode));
    }

    static public boolean lockPickedTroops(String playerCode) throws InterruptedException {
        return tryLock(lockKey(playerCode));
    }

    static public void unlockPickedTroops(String playerCode) {
        unlock(lockKey(playerCode));
    }
}
